package cn.itcast.service;

import cn.itcast.constant.RedisConstant;
import cn.itcast.dao.CheckGroupMapper;
import cn.itcast.dao.CheckItemMapper;
import cn.itcast.dao.SetMealMapper;
import cn.itcast.domain.CheckGroup;
import cn.itcast.domain.CheckItem;
import cn.itcast.domain.Setmeal;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //健壮性判断
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        if (currentPage == null) {
            currentPage = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        //分页助手
        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> page = (Page<Setmeal>) setMealMapper.findByPage(queryPageBean.getQueryString());

        long total = page.getTotal();
        List<Setmeal> result = page.getResult();
        return new PageResult(total, result);
    }

    /**
     * 新增检查套餐
     *
     * @param setMeal
     * @param checkgroupIds
     */
    @Override
    public void insertSetMail(Setmeal setMeal, Integer[] checkgroupIds) {

        setMealMapper.insertSetMeal(setMeal);
        Integer setMealId = setMeal.getId();
        this.insertCheckGroupOfSetMeal(setMealId, checkgroupIds);

        //将图片名称保存到Redis集合中
        String fileName = setMeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, fileName);

    }

    /**
     * 检查套餐的基本信息回显
     *
     * @param id
     * @return
     */
    @Override
    public Setmeal findSetMeal(Integer id) {
        return setMealMapper.findSetMeal(id);
    }

    /**
     * 查询检查套餐对应的检查组的ids
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckGroupIds(Integer id) {
        return setMealMapper.findCheckGroupIds(id);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {

        setMealMapper.deleteSetMealAndCheckGroup(id);

        setMealMapper.deleteSetMeal(id);
    }

    @Override
    public List<Setmeal> getAllSetMeal() {
        return setMealMapper.getAllSetMeal();
    }

    /**
     * 体检套餐的详情展示
     *
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setMealMapper.findSetMeal(id);
        //获取检查套餐中对应的检查组
        List<Integer> checkGroupIds = setMealMapper.findCheckGroupIds(id);
        List<CheckGroup> list = new ArrayList<CheckGroup>();
        for (Integer checkGroupId : checkGroupIds) {
            CheckGroup checkGroup = checkGroupMapper.findByCheckGroupId(checkGroupId);
            List<Integer> checkItemIds = checkGroupMapper.findByCheckItemOfCheckGroup(checkGroupId);
            List<CheckItem> list2 = new ArrayList<CheckItem>();
            for (Integer checkItemId : checkItemIds) {
                CheckItem checkItem = checkItemMapper.findCheckItemById(checkItemId);
                list2.add(checkItem);
            }
            checkGroup.setCheckItems(list2);
            list.add(checkGroup);
        }
        setmeal.setCheckGroups(list);
        return setmeal;
    }

    /**
     * 套餐预约占比统计
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setMealMapper.findSetmealCount();
    }

    /**
     * 修改检查套餐
     *
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {

        //获取简餐套餐的id
        Integer id = setmeal.getId();
        //删除后,新增关联关系
        setMealMapper.delete(id);
        //重建 调用方法
        //修改信息
        setMealMapper.updateSetMeal(setmeal);
        this.insertCheckGroupOfSetMeal(id, checkgroupIds);

    }

    /**
     * 抽取方法:建立检查套餐和检查组的关联关系
     * 使用sql语句调优,批量新增
     *
     * @param setMealId
     * @param checkgroupIds
     */
    public void insertCheckGroupOfSetMeal(Integer setMealId, Integer[] checkgroupIds) {
        List<Map<String, Object>> list = new ArrayList();
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                Map<String, Object> map = new HashMap<>();
                map.put("setMealId", setMealId);
                map.put("checkGroupId", checkgroupId);
                list.add(map);
            }
            setMealMapper.insertCheckGroupOfSetMeal(list);
        }


    }
}
